<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.instanceName"
          clearable
          placeholder="请输入名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发起时间" prop="deployTime">
        <el-date-picker v-model="queryParams.beginDate" clearable
                        placeholder="开始时间"
                        size="small"
                        type="date"
                        value-format="yyyy-MM-dd"
        />
        至
        <el-date-picker v-model="queryParams.endDate" clearable
                        placeholder="结束时间"
                        size="small"
                        type="date"
                        value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['workflow:process:start']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >
          发起流程
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['workflow:process:delete']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >
          删除
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="myProcessList" border @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="流程编号" prop="procInsId" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="流程名称" prop="procDefName" />
      <!--      <el-table-column align="center" label="流程类别" prop="category" width="100px" />-->
      <el-table-column align="center" label="流程版本" width="80px">
        <template slot-scope="scope">
          <el-tag size="medium">
            v{{ scope.row.procDefVersion }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="提交时间" prop="createTime" width="180" />
      <el-table-column align="center" label="流程状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" size="mini">
            进行中
          </el-tag>
          <el-tag v-if="scope.row.status == 2" size="mini" type="success">
            已完成
          </el-tag>
          <el-tag v-if="scope.row.status == 3||scope.row.status == 4" size="mini" type="danger">
            {{ scope.row.statusDesc }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="耗时" prop="duration" width="180" />
      <!--      <el-table-column align="center" label="当前节点" prop="taskName"/>-->
      <!--      <el-table-column align="center" label="办理">-->
      <!--        <template slot-scope="scope">-->
      <!--          <label v-if="scope.row.assigneeName">{{ scope.row.assigneeName }}-->
      <!--            <el-tag size="mini" type="info">{{ scope.row.deptName }}</el-tag>-->
      <!--          </label>-->
      <!--          <label v-if="scope.row.candidate">{{ scope.row.candidate }}</label>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-tickets" @click="handleFlowRecord(scope.row)">
            详情
          </el-button>
          <el-button v-hasPermi="['workflow:process:cancel']" type="text" icon="el-icon-circle-close"
                     @click="handleStop(scope.row)"
          >
            取消申请
          </el-button>
          <el-button v-hasPermi="['workflow:process:delete']" type="text" icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
          <!--          <el-dropdown>-->
          <!--            <span class="el-dropdown-link">-->
          <!--              更多操作<i class="el-icon-arrow-down el-icon&#45;&#45;right" />-->
          <!--            </span>-->
          <!--            <el-dropdown-menu slot="dropdown">-->


          <!--            </el-dropdown-menu>-->
          <!--          </el-dropdown>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.size"
      :page.sync="queryParams.current"
      :total="total"
      @pagination="getList"
    />

    <!-- 发起流程 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="processQueryParams" label-width="68px">
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="processQueryParams.name"
            clearable
            placeholder="请输入名称"
            size="small"
            @keyup.enter.native="queryListDefinition"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="queryListDefinition">
            搜索
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="processLoading" :data="definitionList" border fit>
        <el-table-column align="center" label="流程名称" prop="name" />
        <el-table-column align="center" label="流程版本">
          <template slot-scope="scope">
            <el-tag size="medium">
              v{{ scope.row.version }}
            </el-tag>
          </template>
        </el-table-column>
        <!--        <el-table-column align="center" label="流程分类" prop="category" />-->
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-s-order"
              size="mini"
              type="text"
              @click="handleOpenVersion(scope.row)"
            >
              版本
            </el-button>
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              @click="handleStartProcess(scope.row)"
            >
              发起流程
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="processTotal>0"
        :limit.sync="processQueryParams.size"
        :page.sync="processQueryParams.current"
        :total="processTotal"
        @pagination="queryListDefinition"
      />
    </el-dialog>
    <!--流程版本-->
    <el-dialog title="流程版本" :visible.sync="versionOpen" append-to-body width="60%">
      <el-table :data="versionDefinitionList" border fit>
        <el-table-column align="center" label="流程名称" prop="name" />
        <el-table-column align="center" label="流程版本">
          <template slot-scope="scope">
            <el-tag size="medium">
              v{{ scope.row.version }}
            </el-tag>
          </template>
        </el-table-column><!--        <el-table-column align="center" label="流程分类" prop="category" />-->
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              @click="handleStartProcess(scope.row)"
            >
              发起流程
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="processVersionTotal>0"
        :limit.sync="versionQueryParams.size"
        :page.sync="versionQueryParams.current"
        :total="processVersionTotal"
        @pagination="queryVersionList"
      />
    </el-dialog>
  </div>
</template>

<script>
import {listDefinition, listVersionDefinition} from "@/api/workflow/definition"
import {cancelProcess, deleteProcess, listMyProcess} from "@/api/workflow/process"

export default {
  name: "Deploy",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      processLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      processTotal: 0,
      processVersionTotal: 0,
      // 我发起的流程列表数据
      myProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      versionOpen: false,
      definitionList: [],
      versionDefinitionList: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        instanceName: null,
        beginDate: null,
        endDate: null
      },
      processQueryParams: {
        current: 1,
        size: 10,
        name: null
      },
      versionQueryParams: {
        current: 1,
        size: 10,
        key: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询流程定义列表 */
    getList() {
      this.loading = true
      listMyProcess(this.queryParams).then((response) => {
        this.myProcessList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.procInsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = "发起流程"
      this.queryListDefinition()
    },
    queryListDefinition() {
      listDefinition(this.processQueryParams).then((response) => {
        this.definitionList = response.data.records
        this.processTotal = response.data.total
        this.processLoading = false
      })
    },
    /**  发起流程申请 */
    handleStartProcess(row) {
      this.$router.push({
        path: '/workflow/task/record/start',
        query: {
          deployId: row.deploymentId,
          procDefId: row.id,
          finished: true
        }
      })
    },
    handleOpenVersion(row) {
      this.versionOpen = true
      this.versionQueryParams.key = row.key
      this.queryVersionList()
    },
    queryVersionList() {

      listVersionDefinition(this.versionQueryParams).then((response) => {
        this.versionDefinitionList = response.data.records
        this.processVersionTotal = response.data.total
      })
    },
    /**  取消流程申请 */
    handleStop(row) {
      const params = {
        instanceId: row.procInsId,
        formDataId: row.formDataId
      }
      this.$confirm('是否确定停止"' + row.procDefName + '"流程，停止后流程将直接结束?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return cancelProcess(params)
      }).then(() => {
        this.getList()
        this.msgSuccess("停止成功")
      })
    },
    /** 流程流转记录 */
    handleFlowRecord(row) {
      this.$router.push({
        path: '/workflow/task/record/handle',
        query: {
          procInsId: row.procInsId,
          deployId: row.deployId,
          taskId: row.taskId,
          operated: false
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.procInsId || this.ids
      this.$confirm('是否确认删除流程"' + row.procDefName + '"数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteProcess(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      })
    }
  }
}
</script>

